﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace JQueryPractice.controls
{
    public class SwitchButton : Button
    {
        [Category("Appearance")]
        [DefaultValue("")]
        public string CheckedCss
        {
            get { return ViewState["CheckedCss"] == null ? string.Empty : (string)ViewState["CheckedCss"]; }
            set { ViewState["CheckedCss"] = value;}
        }

        [Category("Appearance")]
        [DefaultValue("")]
        public string NormalCss
        {
            get { return ViewState["NormalCss"] == null ? string.Empty : (string)ViewState["NormalCss"]; }
            set { ViewState["NormalCss"] = value; }
        }

        [Category("Appearance")]
        [DefaultValue(false)]
        public bool Checked
        {
            get { return ViewState["Checked"] == null ? false : (bool)ViewState["Checked"]; }
            set { ViewState["Checked"] = value;}
        }

        public SwitchButton()
        {
            Click += OnButtonClick;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CssClass = Checked ? CheckedCss : NormalCss;
        }

        private void OnButtonClick(object sender, EventArgs e)
        {
            Checked = !Checked;
            CssClass = Checked ? CheckedCss : NormalCss;
        }
    }
}
